package com.yuyou.tas.lesson.controller;

import com.yuyou.tas.lesson.service.IStudentService;
import com.yuyou.tas.lesson.vo.req.ReqStudentAdd;
import com.yuyou.tas.lesson.vo.req.ReqStudentBind;
import com.yuyou.tas.lesson.vo.req.ReqStudentUpdate;
import com.yuyou.tas.service.api.lesson.dto.LessonClassStudentPageDTO;
import com.yuyou.tas.service.api.lesson.dto.StudentDetailDTO;
import com.yuyou.tas.service.api.lesson.dto.StudentPageDTO;
import com.yuyou.tas.service.api.lesson.entity.Student;
import com.yuyou.tas.util.bean.*;
import com.yuyou.tas.util.enums.lesson.StudentEnums;
import com.yuyou.tas.util.tool.DefindBeanUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(value = "StudentController",description="班级学生接口")
@Validated
@RestController
@RequestMapping(value = "/api/student")
public class StudentController extends BaseController {

	@Autowired
	private IStudentService studentService;
		
	@ApiOperation(value = "查询班级学生")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "lessonClassId", value = "班级ID", paramType = "query", required = false),
			@ApiImplicitParam(name = "nameOrNo", value = "学生名字编号", paramType = "query", required = false),
			@ApiImplicitParam(name = "regStatus", value = "状态：0在读， 3退费，4超时撤销，11试听", paramType = "query", required = false)})
	@RequestMapping(value = "/findClaStudentPage", method = {RequestMethod.GET, RequestMethod.POST })
	public ReturnResult<Pagination<LessonClassStudentPageDTO>> findLessonPage(
			HttpServletRequest request,@Validated @ModelAttribute PageParams pageParams,
			Long lessonClassId,String nameOrNo,String regStatus) throws Exception {
		Map<String, Object> conditions = new HashMap<>();
		Pagination<LessonClassStudentPageDTO> queryPgn = new Pagination<>();
		LoginRedisObj user = this.getUserObj(request);
		conditions.put("lessonClassId", lessonClassId);
		conditions.put("nameOrNo", nameOrNo);
		conditions.put("regStatus", regStatus);
		conditions.put("authOrganizationId", user.getOrganizationId());
        queryPgn.setConditions(conditions);
        queryPgn.setPageParams(pageParams);
        int total = studentService.findLessonClassStudentCount(queryPgn);
        queryPgn.setTotal(total);
        queryPgn.toBePage();
        if(total > 0){
            queryPgn.setTotal(total);
            List<LessonClassStudentPageDTO> list = studentService.findLessonClassStudentPageList(queryPgn);
            queryPgn.setList(list);
        }		
		return ReturnResult.success(queryPgn);
	}
	

	@ApiOperation(value = "学生管理列表")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "baseSectionId", value = "年级ID", paramType = "query", required = false),
		@ApiImplicitParam(name = "nameOrNo", value = "学生名字编号", paramType = "query", required = false),
		@ApiImplicitParam(name = "type", value = "学生类型：1：正式，2：非正式", paramType = "query", required = false),})
	@RequestMapping(value = "/findStudentList", method = {RequestMethod.GET, RequestMethod.POST })
	public ReturnResult<Pagination<StudentPageDTO>> findStudentList(
			HttpServletRequest request,
			@Validated @ModelAttribute PageParams pageParams,
			Long baseSectionId,String nameOrNo,String type) throws Exception {	
		Map<String, Object> conditions = new HashMap<>();
		Pagination<StudentPageDTO> queryPgn = new Pagination<>();
		LoginRedisObj user = this.getUserObj(request);
		conditions.put("baseSectionId", baseSectionId);
		conditions.put("nameOrNo", nameOrNo);
		conditions.put("type", type);
		conditions.put("authOrganizationId", user.getOrganizationId());
        queryPgn.setConditions(conditions);
        queryPgn.setPageParams(pageParams);
        int total = studentService.findStudentCount(queryPgn);
        queryPgn.setTotal(total);
        queryPgn.toBePage();
        if(total > 0){
            queryPgn.setTotal(total);
            List<StudentPageDTO> list = studentService.findStudentPageList(queryPgn);
            queryPgn.setList(list);            
            list.stream().forEach(item ->{
            	if(item.getoId()!=null){
            		item.setType(1);
            		if(!StringUtils.isEmpty(item.getPhoneNo())&&(item.getPhoneNo().length()>=11)){
                		String phoneNumber = item.getPhoneNo().substring(0, 3) + "****" + item.getPhoneNo().substring(7, item.getPhoneNo().length());
                		item.setPhoneNo(phoneNumber);
            		}else{
            			item.setPhoneNo("***********");
            		}
            		//备用手机后处理
					if(!StringUtils.isEmpty(item.getSlavePhoneNo())&&(item.getSlavePhoneNo().length()>=11)){
						String phoneNumber = item.getSlavePhoneNo().substring(0, 3) + "****" + item.getSlavePhoneNo().substring(7, item.getSlavePhoneNo().length());
						item.setSlavePhoneNo(phoneNumber);
					}else{
						item.setSlavePhoneNo("***********");
					}
            	}else{
            		item.setType(2);
            	}            	
            });
            
        }		
		return ReturnResult.success(queryPgn);
	}
	
	@ApiOperation(value = "学生详情")
	@ApiImplicitParams({})
	@RequestMapping(value = "/detailStudent", method = {RequestMethod.POST })
	public ReturnResult<StudentDetailDTO> detailStudent(
			HttpServletRequest request,@NotNull(message="学生不能为空")Long id) throws Exception {
		LoginRedisObj obj = this.getUserObj(request);
		StudentDetailDTO res=studentService.detailStudent(id, obj.getOrganizationId());		
		return ReturnResult.success(res);
	}
	
	@ApiOperation(value = "新增学生")
	@ApiImplicitParams({})
	@RequestMapping(value = "/addStudent", method = {RequestMethod.POST })
	public ReturnResult<String> addStudent(
			HttpServletRequest request,@Validated @ModelAttribute ReqStudentAdd reqStudentAdd) throws Exception {
		LoginRedisObj obj = this.getUserObj(request);
		Student student=new Student();
		DefindBeanUtils.copyProperties(reqStudentAdd, student);
		student.setAuthOrganizationId(obj.getOrganizationId());
		ResBoolSimpleInfo<String> res=studentService.addStudent(student);		
		return ReturnResult.returnRes(res);
	}
	
	@ApiOperation(value = "编辑学生")
	@ApiImplicitParams({})
	@RequestMapping(value = "/updateStudent", method = {RequestMethod.POST })
	public ReturnResult<String> updateStudent(
			HttpServletRequest request,@Validated @ModelAttribute ReqStudentUpdate reqStudentUpdate) throws Exception {
		LoginRedisObj obj = this.getUserObj(request);
		Student student=new Student();
		DefindBeanUtils.copyProperties(reqStudentUpdate, student);
		student.setAuthOrganizationId(obj.getOrganizationId());
		ResBoolSimpleInfo<String> res=studentService.updateStudent(student);
		return ReturnResult.returnRes(res);
	}
	
	
	@ApiOperation(value = "绑定学生")
	@ApiImplicitParams({})
	@RequestMapping(value = "/bindStudent", method = {RequestMethod.POST })
	public ReturnResult<String> bindStudent(
			HttpServletRequest request,@Validated @ModelAttribute ReqStudentBind reqStudentBind) throws Exception {	
		LoginRedisObj obj = this.getUserObj(request);
		ResBoolSimpleInfo<String> res=studentService.bindStudent(reqStudentBind.getLessonClassId(), reqStudentBind.getStudentIdList(),obj.getOrganizationId(), StudentEnums.regStatus.potential.getValue());
		return ReturnResult.returnRes(res);
	}
	
	
	
	

}
